package com.taobao.ui.widget;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.taobao.action.AddSmallerAction;
import com.taobao.action.ColumnAction;
import com.taobao.action.PreUpdateSmallerAction;
import com.taobao.action.SmallerPriorityAction;
import com.taobao.action.UpdateSmallerAction;
import com.taobao.action.ui.ButtonAction;
import com.taobao.action.ui.MouseDoubleClickAction;
import com.taobao.action.ui.MouseRightAction;
import com.taobao.pojo.Smaller;
import com.taobao.pojo.UserInfo;
import com.taobao.pojo.ui.ButtonNO;
import com.taobao.tools.Binding;
import com.taobao.ui.menu.CommonMenu;

public class SmallerWidget extends CommonWidget {

    private Table table;
    private UpdateSmallerWidget updateSmallerWidget;

    @Override
    public String getID() {
        return "com.taobao.ui.widget.SmallerWidget";
    }

    /**
     * Create the composite.
     * 
     * @param parent
     * @param style
     */
    public SmallerWidget(Composite parent, int style) {
        super(parent, style);
        GridLayout gridLayout = new GridLayout(1, false);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        setLayout(gridLayout);

        table = new Table(this, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        table.setMenu(new CommonMenu(table));
        table.addMouseListener(new MouseRightAction(new ColumnAction(), getThis(SmallerWidget.class), table));
        table.addMouseListener(new MouseDoubleClickAction(new PreUpdateSmallerAction(), getThis(SmallerWidget.class), table));
        Binding.binding(getThis(SmallerWidget.class), table, Binding.GROUP_LIST, UserInfo.class, "smallers");

        TableColumn tableColumn = new TableColumn(table, SWT.NONE);
        tableColumn.setWidth(25);

        TableColumn tableColumn_0 = new TableColumn(table, SWT.NONE);
        tableColumn_0.setWidth(50);
        tableColumn_0.setText("\u7F16\u53F7");
        tableColumn_0.setToolTipText("no");

        TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
        tableColumn_1.setWidth(160);
        tableColumn_1.setText("\u5C0F\u53F7\u540D\u79F0");
        tableColumn_1.setToolTipText("smallerName");

        TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
        tableColumn_2.setWidth(60);
        tableColumn_2.setText("\u4F18\u5148\u7EA7");
        tableColumn_2.setToolTipText("priorityLabel");

        TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
        tableColumn_4.setWidth(100);
        tableColumn_4.setText("\u5BC6\u7801");
        tableColumn_4.setToolTipText("tbPwd");

        TableColumn tblclmnTid = new TableColumn(table, SWT.NONE);
        tblclmnTid.setWidth(100);
        tblclmnTid.setText("TID");
        tblclmnTid.setToolTipText("tid");

        updateSmallerWidget = new UpdateSmallerWidget(this, SWT.NONE);
        updateSmallerWidget.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Composite composite_0 = new Composite(this, SWT.NONE);
        composite_0.setLayout(new GridLayout(2, false));
        composite_0.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        
        Composite composite = new Composite(composite_0, SWT.LEFT_TO_RIGHT);
        composite.setLayout(new GridLayout(7, false));
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        
        Button button = new Button(composite, SWT.NONE);
        button.setToolTipText("\u6DFB\u52A0\u5C0F\u53F7");
        GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gridData.widthHint = 70;
        button.setLayoutData(gridData);
        button.setText("\u6DFB\u52A0");
        button.addSelectionListener(new ButtonAction(new AddSmallerAction(), getThis(SmallerWidget.class), ButtonNO.ADD_SMALLER));
        
        Button button_6 = new Button(composite, SWT.NONE);
        button_6.setToolTipText("\u66F4\u65B0\u5BC6\u7801\u548CTID");
        GridData gridData_6 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gridData_6.widthHint = 70;
        button_6.setLayoutData(gridData_6);
        button_6.setText("\u66F4\u65B0");
        button_6.addSelectionListener(new ButtonAction(new UpdateSmallerAction(), getThis(SmallerWidget.class), null));

        Button button_1 = new Button(composite, SWT.NONE);
        button_1.setToolTipText("\u5220\u9664\u5C0F\u53F7");
        GridData gridData_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gridData_1.widthHint = 70;
        button_1.setLayoutData(gridData_1);
        button_1.setText("\u5220\u9664");
        
        Composite composite_1 = new Composite(composite_0, SWT.RIGHT_TO_LEFT);
        composite_1.setLayout(new GridLayout(7, false));
        composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
        
        Button button_2 = new Button(composite_1, SWT.NONE);
        button_2.setToolTipText("\u7981\u7528\u4F46\u4E0D\u5220\u9664\u5C0F\u53F7");
        GridData gridData_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gridData_2.widthHint = 70;
        button_2.setLayoutData(gridData_2);
        button_2.setText("\u7981\u7528");
        button_2.addSelectionListener(new ButtonAction(new SmallerPriorityAction(), getThis(SmallerWidget.class), ButtonNO.FORBID));
        
        Button button_4 = new Button(composite_1, SWT.NONE);
        button_4.setToolTipText("\u4F18\u5148\u7EA7\u4F4E");
        GridData gridData_4 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gridData_4.widthHint = 70;
        button_4.setLayoutData(gridData_4);
        button_4.setText("\u4F4E");
        button_4.addSelectionListener(new ButtonAction(new SmallerPriorityAction(), getThis(SmallerWidget.class), ButtonNO.LOW));

        Button button_3 = new Button(composite_1, SWT.NONE);
        button_3.setToolTipText("\u4F18\u5148\u7EA7\u4E2D");
        GridData gridData_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gridData_3.widthHint = 70;
        button_3.setLayoutData(gridData_3);
        button_3.setText("\u4E2D");
        button_3.addSelectionListener(new ButtonAction(new SmallerPriorityAction(), getThis(SmallerWidget.class), ButtonNO.MIDDLE));

        Button button_5 = new Button(composite_1, SWT.NONE);
        button_5.setToolTipText("\u4F18\u5148\u7EA7\u9AD8");
        GridData gridData_5 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        gridData_5.widthHint = 70;
        button_5.setLayoutData(gridData_5);
        button_5.setText("\u9AD8");
        button_5.addSelectionListener(new ButtonAction(new SmallerPriorityAction(), getThis(SmallerWidget.class), ButtonNO.HIGH));

    }

    public SmallerWidget(Composite parent, int style, UserInfo userInfo) {
        this(parent, style);
        fillWidget(userInfo);
    }

    public List<Smaller> getSelectSmaller() {
        List<Smaller> selected = new ArrayList<Smaller>();
        TableItem[] items = table.getItems();
        for (TableItem item : items) {
            if (item.getChecked()) {
                Smaller smaller = (Smaller) item.getData();
                selected.add(smaller);
            }
        }
        return selected;
    }

    public UpdateSmallerWidget getUpdateSmallerWidget() {
        return updateSmallerWidget;
    }
}
